hfrApp.registerCtrl("AdminListTopicController", function ($scope, $http, $log, $window, $translate, $rootScope, $timeout, HfrConfig, HfrStorage, HfrInit) {
	
	/*
	 * ---------------------- Init Controller ---------------------------
	 */
	//Init message alert
	HfrInit.message();
	
	//Delay content for display after 0,8 second
	$timeout(function () {
		$scope.showViewContent = true;
	}, 800);
	
	$scope.checkTopics = {};
	$scope.showSitemapOfTopic = {};
	$scope.showContentSitemapOfTopic = {};
	$scope.sitemapsOfTopics = {};
	
	//Set default value
	$scope.limit = 10;
	
	//Set default paginatate for sitemaps
	$scope.sitemapShowNext = false;
	$scope.sitemapShowPrev = false
	if ($scope.sitemapPage == undefined) {
		$scope.sitemapPage = 1;
	}
	
	//Set default paginatate for sitemapOfTopic
	$scope.sitemapOfTopicShowNext = {};
	$scope.sitemapOfTopicShowPrev = {};
	$scope.sitemapOfTopicPage = {};
	
	//Get all topics
	$scope.topics = [];
	$scope.getAllTopic = function () {
		$scope.showTopics = false;
		
		$http.get(HfrConfig.ApiServiceUrl.topicsUrlApi)
			.success(function (data) {
				if (data.status == 200) {
					angular.forEach(data.response.topics, function(topic, key) {
						$scope.topics.push(topic);
					});
					//$scope.topics = data.response.topics;
				} else {
					$rootScope.message.error = true;
					$rootScope.message.text = data.message;
				}
				$scope.showTopics = true;
			})
			.error(function (data) {
				$scope.topics = [];
				$scope.showTopics = true;
				$rootScope.message.error = true;
				$rootScope.message.text = $translate.instant("message.error.connect");
			});
	};
	
	$scope.getAllTopic();
	//Get all sitemaps
	$scope.getAllSitemap = function () {
		var sitemapsUrl = HfrConfig.ApiServiceUrl.sitemapsUrlApi + "?page=" + $scope.sitemapPage;
		if ($scope.sitemapKeySearch != undefined) {
			sitemapsUrl = HfrConfig.ApiServiceUrl.sitemapsUrlApi + "?search=" + $scope.sitemapKeySearch + "&page=" + $scope.sitemapPage;
		}
		
		$scope.showSitemaps = false;
		
		$http.get(sitemapsUrl)
			.success(function (data) {
				if (data.status == 200) {
					$scope.sitemaps = data.response.sitemaps;
					$scope.sitemapTotal = $scope.sizeOf(data.response.sitemaps);
					//Check show paginator
					if ($scope.sitemapTotal >= $scope.limit) {
						$scope.sitemapShowNext = true;
					} else {
						$scope.sitemapShowNext = false;
					}
					
					if ($scope.sitemapPage > 1) {
						$scope.sitemapShowPrev = true;
					} else {
						$scope.sitemapShowPrev = false;
					}
				} else {
					$scope.sitemapTotal = 0;
					$rootScope.message.error = true;
					$rootScope.message.text = data.message;
				}
				$scope.showSitemaps = true;
			})
			.error(function (data) {
				$scope.sitemaps = {};
				$scope.showSitemaps = true;
				$rootScope.message.error = true;
				$rootScope.message.text = $translate.instant("message.error.connect");
			});
	};
	$scope.getAllSitemap();
	
	/*
	 * ---------------------- Process Controller ---------------------------
	 */
	//Them hoac xoa topic cua mot sitemap
	$scope.changeSelect = function() {
		$scope.data = {
			sitemaps:[]
		};
		
		if ($scope.sitemapSelected === undefined) {
			$rootScope.message.error = true;
			$rootScope.message.text = $translate.instant("message.admin.listTopic.errorSelectSitemap");
		} else {
			//Get topic check
			angular.forEach($scope.topics, function(topic) {
				if ($scope.checkTopics[topic.id]) {
					$scope.data.sitemaps.push({
						sitemapId: $scope.sitemapSelected,
						topicId: topic.id,
					});
				}
			});
			
			//Check if size data.sitemaps zero
			if ($scope.sizeOf($scope.data.sitemaps) == 0) {
				$scope.data.sitemaps.push({
					sitemapId: $scope.sitemapSelected,
					topicId: "",
				});
			}
			
			//Submit topic to server
			var addTopicSitemapUrl = HfrConfig.ApiServiceUrl.sitemapsUrlApi + "/topics";
			$http.post(addTopicSitemapUrl, $scope.data)
				.success(function (data) {
					$scope.getAllSitemap();
					if (data.status != 200) {
						$rootScope.message.error = true;
						$rootScope.message.text = data.message;
					}
					
				}).
			    error(function(data) {
			    	$rootScope.message.error = true;
					$rootScope.message.text = $translate.instant("message.error.connect");
			    });
		}
		
	};
	
	//Hien thi danh sach sitemap thuoc topic
	$scope.showSitemapOfTopic = function (topicId) {
		if ($scope.showSitemapOfTopic[topicId]) {
			$scope.showSitemapOfTopic[topicId] = false;
		} else {
			$scope.showSitemapOfTopic[topicId] = true;
		}
		
		//Lay danh sach sitemap cua topic
		if ($scope.showSitemapOfTopic[topicId]) {
			if ($scope.sitemapOfTopicShowNext[topicId] == undefined) {
				$scope.sitemapOfTopicShowNext[topicId] = false;
			}
			if ($scope.sitemapOfTopicShowPrev[topicId] == undefined) {
				$scope.sitemapOfTopicShowPrev[topicId] = false;
			}
			
			if ($scope.sitemapOfTopicPage[topicId] == undefined) {
				$scope.sitemapOfTopicPage[topicId] = 1;
			}
			
			$scope.getSitemapOfTopic(topicId);
		}

		return false;
	};
	
	//Lay danh sach sitemap thuoc topic
	$scope.getSitemapOfTopic = function (topicId) {
		var sitemapOfTopicUrl = HfrConfig.ApiServiceUrl.sitemapsUrlApi + "?topicId=" + topicId + "&page=" + $scope.sitemapOfTopicPage[topicId];
		$scope.showContentSitemapOfTopic[topicId] = false;
		$http.get(sitemapOfTopicUrl)
			.success(function (data) {
				if (data.status == 200) {
					$scope.sitemapsOfTopics[topicId] = data.response.sitemaps;
					$scope.totalSitemapOfTopic = $scope.sizeOf(data.response.sitemaps);
					
					//Check show paginator
					if ($scope.totalSitemapOfTopic >= $scope.limit) {
						$scope.sitemapOfTopicShowNext[topicId] = true;
					} else {
						$scope.sitemapOfTopicShowNext[topicId] = false;
					}
					
					if ($scope.sitemapOfTopicPage[topicId] > 1) {
						$scope.sitemapOfTopicShowPrev[topicId] = true;
					} else {
						$scope.sitemapOfTopicShowPrev[topicId] = false;
					}
					
				} else {
					$rootScope.message.error = true;
					$rootScope.message.text = data.message;
				}
				
				$scope.showContentSitemapOfTopic[topicId] = true;
			})
			.error(function (data) {
				$scope.sitemapsOfTopics[topicId] = {};
				$scope.showContentSitemapOfTopic[topicId] = true;
			});
	}
	
	//Chon sitemap de tuong tac
	$scope.choiseSitemap = function (sitemapUrl) {
		if ($scope.sitemapSelected != sitemapUrl) {
			$scope.sitemapSelected = sitemapUrl;
			//Check topic of sitemaps
			if ($scope.sitemaps[sitemapUrl] !== undefined)  {
				angular.forEach($scope.topics, function(topic) {
					$scope.checkTopics[topic.id] = false;
				});
				
				angular.forEach($scope.sitemaps[sitemapUrl].topics, function(topic, key) {
					$scope.checkTopics[topic.id] = true;
				});
			}
		}
	};
	
	//Remove topic on sitemap
	$scope.removeTopic = function (topicId) {
		if ($scope.sitemapSelected === undefined) {
			return false;
		} else {
			$scope.checkTopics[topicId] = false;
			$scope.changeSelect();
		}
	};
	
	//prevSitemapPage
	$scope.prevSitemapPage = function () {
		if ($scope.sitemapShowPrev) {
			$scope.sitemapPage -= 1;
			$scope.getAllSitemap();
		}
	};
	
	//nextSitemapPage
	$scope.nextSitemapPage = function () {
		if ($scope.sitemapShowNext) {
			$scope.sitemapPage += 1;
			$scope.getAllSitemap();
		}
	};
	
	//prevSitemapOfTopicPage
	$scope.prevSitemapOfTopicPage = function (topicId) {
		if ($scope.sitemapOfTopicShowPrev[topicId]) {
			$scope.sitemapOfTopicPage[topicId] -= 1;
			$scope.getSitemapOfTopic(topicId);
		}
	};
	
	//nextSitemapOfTopicPage
	$scope.nextSitemapOfTopicPage = function (topicId) {
		if ($scope.sitemapOfTopicShowNext[topicId]) {
			$scope.sitemapOfTopicPage[topicId] += 1;
			$scope.getSitemapOfTopic(topicId);
		}
	};
	
	//add new topic
	$scope.addTopic = function (event) {
		if (event.which == 13) {
			var topicName = $scope.topicName;
			
			var addTopicUrl = HfrConfig.ApiServiceUrl.topicsUrlApi;
			$scope.data = {
				topic: {name: topicName}
			}
			$http.post(addTopicUrl, $scope.data)
				.success(function (data) {
					$scope.getAllTopic();
					if (data.status == 200) {
						$rootScope.message.success = true;
						$rootScope.message.text = data.message;
					} else {
						$rootScope.message.error = true;
						$rootScope.message.text = data.message;
					}
					
				}).
			    error(function(data) {
			    	$rootScope.message.error = true;
					$rootScope.message.text = $translate.instant("message.error.connect");
			    });
			
			//Set input emtpy
			$scope.topicName = "";
		}
	};
	
	//Search sitemap
	$scope.searchSitemap = function (event) {
		if (event.which == 13) {
			if ($scope.keyword == undefined || $scope.keyword == "") {
				if ($scope.sitemapKeySearch != undefined) {
					$scope.sitemapKeySearch = undefined;
					$scope.sitemapShowNext = false;
					$scope.sitemapShowPrev = false
					$scope.sitemapPage = 1;					
				}
			} else {
				if ($scope.sitemapKeySearch == undefined || $scope.sitemapKeySearch != $scope.keyword) {
					$scope.sitemapKeySearch = $scope.keyword;
					$scope.sitemapShowNext = false;
					$scope.sitemapShowPrev = false
					$scope.sitemapPage = 1;
				} 
			}
			
			
			$scope.getAllSitemap();
		}
	};
	
	//Count size
	$scope.sizeOf = function(obj) {
	    return Object.keys(obj).length;
	};
});